home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / xarchie-2.0.9 / file-panel.c < prev    next >
C/C++ Source or Header  |  1995-06-18  |  6KB  |  272 lines

  1. /*
  2.  * file-panel.c : The save-load-write panel
  3.  *
  4.  * George Ferguson, ferguson@cs.rochester.edu, 23 Apr 1993.
  5.  * 14 May 1993: Fixed #endif followed by non-comment.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <X11/Intrinsic.h>
  10. #include <X11/Shell.h>
  11. #include <X11/StringDefs.h>
  12. #include <X11/Xaw/Form.h>
  13. #include <X11/Xaw/MenuButton.h>
  14. #include <X11/Xaw/Label.h>
  15. #ifdef FILECHOOSER
  16. # include <FChooser.h>
  17. #endif
  18. #include "config.h"
  19. #ifdef HAVE_SYS_PARAM_H
  20. #include <sys/param.h>
  21. #endif
  22. #include "xarchie.h"
  23. #include "db.h"
  24. #include "browser.h"
  25. #include "appres.h"
  26. #include "saveload.h"
  27. #include "file-panel.h"
  28. #include "fchooser.h"
  29. #include "status.h"
  30. #include "xutil.h"
  31. extern DbEntry *db;
  32.  
  33. /*
  34.  * Functions defined here
  35.  */
  36. void initFilePanelActions();
  37. void updateFileWriteMode();
  38. void setFileShellState();
  39.  
  40. static void fileSaveAction(),fileLoadAction(),fileWriteAction();
  41. static void popupFilePanel(),initFileWidgets();
  42. static void filePanelOk(),filePanelCancel();
  43. /*
  44. static FileChooserOkProc filePanelOk;
  45. static FileChooserCancelProc filePanelCancel;
  46. */
  47. static void writeModeWidgetsShown();
  48.  
  49. /*
  50.  * Data defined here
  51.  */
  52. static FileChooserInfo *fcinfo;
  53. static Widget fileShell,filePanelLabel;
  54. static Widget filePanelWriteModeButton,filePanelWriteModeLabel;
  55. static Boolean fileGoing,isPoppedUp;
  56. static int fileOp;
  57.  
  58. static XtActionsRec actionTable[] = {
  59.     { "file-save",        fileSaveAction },
  60.     { "file-load",        fileLoadAction },
  61.     { "file-write",        fileWriteAction },
  62. };
  63.  
  64. void
  65. initFilePanelActions()
  66. {
  67.     XtAppAddActions(appContext,actionTable,XtNumber(actionTable));
  68. }
  69.  
  70. /*    -    -    -    -    -    -    -    -    */
  71. /* Action Procedures */
  72.  
  73. #define ACTION_PROC(NAME)    void NAME(w,event,params,num_params) \
  74.                     Widget w; \
  75.                     XEvent *event; \
  76.                     String *params; \
  77.                     Cardinal *num_params;
  78.  
  79. /*ARGSUSED*/
  80. static
  81. ACTION_PROC(fileSaveAction)
  82. {
  83.     popupFilePanel(FILE_SAVE);
  84. }
  85.  
  86. /*ARGSUSED*/
  87. static
  88. ACTION_PROC(fileLoadAction)
  89. {
  90.     popupFilePanel(FILE_LOAD);
  91. }
  92.  
  93. /*ARGSUSED*/
  94. static
  95. ACTION_PROC(fileWriteAction)
  96. {
  97.     popupFilePanel(FILE_WRITE);
  98. }
  99.  
  100. static void
  101. popupFilePanel(op)
  102. int op;
  103. {
  104.     /* Ignore if the browser is being changed */
  105.     if (getBrowserState() != BROWSER_READY) {
  106.     XBell(display,0);
  107.     return;
  108.     }
  109.     /* Create if this is the first time */
  110.     if (fileShell == NULL) {
  111.     setBusyStatus(True);
  112.     initFileWidgets();
  113.     setBusyStatus(False);
  114.     }
  115. #ifdef FILECHOOSER
  116.       else {
  117.       status0("Re-initializing File Selector...");
  118.       XfwfFileChooserRefresh((XfwfFileChooserWidget)(fcinfo->fcw));
  119.     }
  120. #endif
  121.     switch (op) {
  122.     case FILE_SAVE:
  123.         setWidgetLabel(filePanelLabel,"Save to file:");
  124.         writeModeWidgetsShown(False);
  125.         break;
  126.     case FILE_LOAD:
  127.         setWidgetLabel(filePanelLabel,"Load from file:");
  128.         writeModeWidgetsShown(False);
  129.         break;
  130.     case FILE_WRITE:
  131.         setWidgetLabel(filePanelLabel,"Write to file:");
  132.         writeModeWidgetsShown(True);
  133.         break;
  134.     }
  135.     fileOp = op;
  136.     if (isPoppedUp) {
  137.     XRaiseWindow(display,XtWindow(fileShell));
  138.     } else {
  139.     isPoppedUp = True;
  140.     XtPopup(fileShell,XtGrabNone);
  141.     }
  142. }
  143.  
  144. static void
  145. initFileWidgets()
  146. {
  147.     Widget form;
  148.     Arg args[1];
  149.  
  150.     fileShell = XtCreatePopupShell("filePanelShell",topLevelShellWidgetClass,
  151.                    toplevel,NULL,0);
  152.     form = XtCreateManagedWidget("filePanelForm",formWidgetClass,
  153.                  fileShell,NULL,0);
  154.     filePanelLabel = XtCreateManagedWidget("filePanelLabel",labelWidgetClass,
  155.                        form,NULL,0);
  156.     filePanelWriteModeButton =
  157.     XtCreateManagedWidget("filePanelWriteModeButton",menuButtonWidgetClass,
  158.                   form,NULL,0);
  159.     filePanelWriteModeLabel =
  160.     XtCreateManagedWidget("filePanelWriteModeLabel",labelWidgetClass,
  161.                   form,NULL,0);
  162.     fcinfo = createFileChooser(fileShell,form,"filePanel",filePanelOk,
  163.                    filePanelCancel,(XtPointer)fileShell);
  164.     /* Adjust vertical layout */
  165.     XtSetArg(args[0],XtNfromVert,filePanelWriteModeButton);
  166. #ifdef FILECHOOSER
  167.     XtSetValues(fcinfo->fcw,args,1);
  168. #else
  169.     XtSetValues(fcinfo->text,args,1);
  170. #endif
  171.     /* Realize them all */
  172.     XtRealizeWidget(fileShell);
  173.     /* Register window for WM */
  174.     (void)XSetWMProtocols(XtDisplay(fileShell),XtWindow(fileShell),
  175.               &WM_DELETE_WINDOW,1);
  176.     updateFileWriteMode(appResources.fileWriteOnePerLine);
  177. }
  178.  
  179. static void
  180. writeModeWidgetsShown(state)
  181. Boolean state;
  182. {
  183.     if (state) {
  184.     XtMapWidget(filePanelWriteModeButton);
  185.     XtMapWidget(filePanelWriteModeLabel);
  186.     } else {
  187.     XtUnmapWidget(filePanelWriteModeButton);
  188.     XtUnmapWidget(filePanelWriteModeLabel);
  189.     }
  190. }
  191.  
  192. void
  193. setFileShellState(state)
  194. int state;
  195. {
  196.     if (!isPoppedUp)
  197.     return;
  198.     switch (state) {
  199.     case NormalState:
  200.         XtMapWidget(fileShell);
  201.         break;
  202.     case IconicState:
  203.         XtUnmapWidget(fileShell);
  204.         break;
  205.     }
  206. }
  207.  
  208. /*    -    -    -    -    -    -    -    -    */
  209. /* Callbacks from the FileChooser */
  210.  
  211. /*ARGSUSED*/
  212. static void
  213. filePanelOk(fcinfo,filename,client_data)
  214. FileChooserInfo *fcinfo;
  215. char *filename;
  216. XtPointer client_data;        /* shell Widget */
  217. {
  218.     Widget shell = (Widget)client_data;
  219.     int status;
  220.  
  221.     if (fileGoing) {
  222.     XBell(display,0);
  223.     return;
  224.     }
  225.     fileGoing = True;
  226.     switch (fileOp) {
  227.     case FILE_SAVE:
  228.         status = save(db,filename);
  229.         break;
  230.     case FILE_LOAD:
  231.         status = load(db,filename);
  232.         break;
  233.     case FILE_WRITE:
  234.         status = writeToFile(db,filename,appResources.fileWriteOnePerLine);
  235.         break;
  236.     }
  237.     fileGoing = False;
  238.     if (status) {
  239.     XtPopdown(shell);
  240.     isPoppedUp = False;
  241.     }
  242. }
  243.  
  244. /*ARGSUSED*/
  245. static void
  246. filePanelCancel(fcinfo,client_data)
  247. FileChooserInfo *fcinfo;
  248. XtPointer client_data;        /* shell Widget */
  249. {
  250.     Widget shell = (Widget)client_data;
  251.  
  252.     if (fileGoing) {
  253.     XBell(display,0);
  254.     return;
  255.     }
  256.     XtPopdown(shell);
  257.     isPoppedUp = False;
  258. }
  259.  
  260. /*    -    -    -    -    -    -    -    -    */
  261. /* Used by the menus */
  262.  
  263. void
  264. updateFileWriteMode(flag)
  265. Boolean flag;
  266. {
  267.     if (flag)
  268.     setWidgetLabel(filePanelWriteModeLabel,"One entry per line");
  269.     else
  270.     setWidgetLabel(filePanelWriteModeLabel,"Pretty-printed");
  271. }
  272.